<?php

namespace common\widgets\pages;

use common\widgets\AjaxModal;
use Yii;
use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\ServerErrorHttpException;
use yii\web\View;

class AdminPage extends Component
{

    /**
     * @var string
     */
    protected $themeViewPath = "@common/views/widgets/pages/";

    /**
     * @var string
     */
    protected $pageViewName = '';

    /**
     * @var
     */
    protected $controller;

    /**
     * @var
     */
    public $tabs;

    /**
     * @var
     */
    private $_view;

    const CONTROLLER = 'controller';
    const TABS       = 'tabs';

    public function addTab($label, $content, $options = [])
    {
        $this->tabs["items"][] = ArrayHelper::merge(['label' => $label, 'content' => $content], $options);
    }

    /**
     * @return mixed
     */
    public function getController()
    {
        if ( ! $this->controller)
        {
            $this->controller = Yii::$app->controller;
        }
        return $this->controller;
    }

    /**
     * @param mixed $controller
     */
    public function setController(Controller $controller)
    {
        $this->controller = $controller;
    }

    protected function getPageViewPath()
    {
        if (empty($this->pageViewName) || empty($this->themeViewPath))
        {
            throw new ServerErrorHttpException("You must set the pageViewName and themeViewPath.");
        }

        return $this->themeViewPath . $this->pageViewName;
    }

    /**
     * Returns the view object that can be used to render views or view files.
     * The [[render()]] and [[renderFile()]] methods will use
     * this view object to implement the actual view rendering.
     * If not set, it will default to the "view" application component.
     *
     * @return \yii\web\View the view object that can be used to render views or view files.
     */
    public function getView()
    {
        if ($this->_view === null)
        {
            $this->_view = Yii::$app->getView();
        }

        return $this->_view;
    }

    /**
     * Sets the view object to be used by this widget.
     *
     * @param View $view the view object that can be used to render views or view files.
     */
    public function setView($view)
    {
        $this->_view = $view;
    }

    /**
     * 快速创建一个AjaxModalBtn
     *
     * @param           $url
     * @param           $label
     * @param bool|true $refresh
     * @return array
     * @throws \Exception
     */
    public static function genAjaxModalBtn($url, $label, $refresh = true)
    {
        return [
            'content' => AjaxModal::widget([
                'url'             => Url::to($url),
                'divideToBodyEnd' => true,
                'toggleButton'    => ["label" => $label, 'tag' => "a"],
                "refresh"         => $refresh,
            ])];
    }

    /**
     * 快速创建一个AjaxModal窗口的返回
     *
     * @param $title
     * @param $content
     * @return string
     */
    public static function genAjaxModalReturn($title, $content)
    {
        return AjaxModal::createModal([
            'title'   => $title,
            "content" => $content,
        ]);
    }

}